package repository

import (
	"context"
	v1 "gin-vben-admin/api/v1"
	"gin-vben-admin/internal/model"
)

type SysRoleRepository interface {
	PageList(ctx context.Context, req *v1.SysRoleListReq, preloads ...string) (int64, []*model.SysRole, error)
	Create(ctx context.Context, m *model.SysRole) error
	Update(ctx context.Context, m *model.SysRole, fields ...string) error
	Delete(ctx context.Context, ids ...int64) error
	ExistsByName(ctx context.Context, roleName string) (bool, error)
	CreateRoleMenus(ctx context.Context, rms []model.SysRoleMenu) error
	CreatePermissions(ctx context.Context, ps []*model.SysPermission) error
	//Update(ctx context.Context, m *model.SysMenu) error
	GetById(ctx context.Context, id int64) (*model.SysRole, error)
	DeleteRoleMenuByRoleId(ctx context.Context, roleId int64) error
	DeletePermissionByRoleId(ctx context.Context, roleId int64) error
	//GetListByPid(ctx context.Context, menuId int64) (menuList []*model.SysMenu, err error)
	//ExistsPermission(ctx context.Context, permission string, excludeId int64) bool
	//CreateMenuApis(ctx context.Context, m []*model.SysMenuApi) error
	//GetCountByPid(ctx context.Context, pid int64) (total int64, err error)
	//Delete(ctx context.Context, menuId int64) error
	//DeleteMenuApi(ctx context.Context, menuId int64) error
}

func NewSysRoleRepository(r *Repository) SysRoleRepository {
	return &sysRoleRepository{
		Repository: r,
	}
}

type sysRoleRepository struct {
	*Repository
}

func (r *sysRoleRepository) PageList(ctx context.Context, req *v1.SysRoleListReq, preloads ...string) (total int64, list []*model.SysRole, err error) {
	list = make([]*model.SysRole, 0)
	_db := r.DB(ctx).Model(model.SysRole{})
	if req.Kw != "" {
		_db = _db.Where("role_name like ?", req.Kw)
	}
	if req.Status != 0 {
		_db = _db.Where("status = ?", req.Status)
	}
	err = _db.Count(&total).Error
	if err != nil || total == 0 {
		return
	}
	_db = _db.Scopes(req.PageQuery()).Order("sort asc")
	for i := range preloads {
		_db = _db.Preload(preloads[i])
	}
	err = _db.Find(&list).Error
	return
}

func (r *sysRoleRepository) ExistsByName(ctx context.Context, roleName string) (bool, error) {
	total := int64(0)
	err := r.DB(ctx).Model(model.SysRole{}).Where("role_name").Count(&total).Error
	return total > 0, err
}

func (r *sysRoleRepository) Create(ctx context.Context, m *model.SysRole) error {
	return r.DB(ctx).Create(m).Error
}

func (r *sysRoleRepository) GetById(ctx context.Context, id int64) (m *model.SysRole, err error) {
	err = r.DB(ctx).First(&m, id).Error
	return
}

func (r *sysRoleRepository) Update(ctx context.Context, m *model.SysRole, fields ...string) error {
	_db := r.DB(ctx)
	if len(fields) > 0 {
		_db = _db.Select(fields)
	}
	return _db.Updates(m).Error
}

func (r *sysRoleRepository) Delete(ctx context.Context, ids ...int64) error {
	return r.DB(ctx).Delete(model.SysRole{}, ids).Error
}

func (r *sysRoleRepository) CreateRoleMenus(ctx context.Context, rms []model.SysRoleMenu) error {
	return r.DB(ctx).Model(model.SysRoleMenu{}).Create(&rms).Error
}

func (r *sysRoleRepository) CreatePermissions(ctx context.Context, ps []*model.SysPermission) error {
	return r.DB(ctx).Model(model.SysPermission{}).Create(&ps).Error
}

func (r *sysRoleRepository) DeleteRoleMenuByRoleId(ctx context.Context, roleId int64) error {
	return r.DB(ctx).Where("role_id = ?", roleId).Delete(model.SysRoleMenu{}).Error
}

func (r *sysRoleRepository) DeletePermissionByRoleId(ctx context.Context, roleId int64) error {
	return r.DB(ctx).Where("role_id = ?", roleId).Delete(model.SysPermission{}).Error
}
